﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TarreOA.TarreOADAL;
using TarreOA.TarreOAModels;
using System.Data.SqlClient;
public partial class page_NewsManagementIframe : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //首先得到用户权限 
        if (Session["user"] == null)
        {
            Session["ShowMessage"] = "用户未登录，请重新登录!";
            Session["Backurl"] = "Login.aspx";
            Response.Redirect("~/Message.aspx");
        }
        else
        {
            TarreOA.TarreOAModels.User sucessUser = (User)Session["user"];
            bool userFlag = false;
            if (sucessUser.PopedomGroup.PopedomList != null)
            {
                foreach (Popedom var in sucessUser.PopedomGroup.PopedomList)
                {
                    if (var.PopedomId == 18)
                    {
                        userFlag = true;
                        break;
                    }
                }

            }
            if (!userFlag)
            {
                Session["ShowMessage"] = "用户没有该权限，请与管理员联系!";
                Session["Backurl"] = "Right.aspx";
                Response.Redirect("~/Message.aspx");
            }

        }
        if (!IsPostBack)
        {

            //
           this.getDataSet();

        }
        
    }
    protected DataSet getDataSet()
    {
        //首先得到用户权限 



        //

        

        SqlConnection conn = SQLManager.GetConn();
        SqlDataAdapter myda = new SqlDataAdapter("SELECT Title, Id, newsDate, State, SectionId, IssueUser FROM News order by newsDate desc", conn);
        DataSet myds = new DataSet();
        myda.Fill(myds, "lis");
        GVNewsManager.DataSource = myds;
        //        GridView1.DataKeyNames = new string[] { "id" };
        GVNewsManager.DataBind();
        conn.Close();
        return myds;

    }
    protected void getDataSet_Select(String str)
    {

    }
    protected void ButSelect_Click(object sender, EventArgs e)
    {
        //首先得到用户权限 和用户名   修改时注意修改sql语句



        //


        SqlConnection conn = SQLManager.GetConn();
        SqlDataAdapter myda = new SqlDataAdapter("SELECT Title, Id, newsDate, State, SectionId, IssueUser FROM News where title like '%" + TBSelect.Text + "%' order by newsDate desc", conn);
        DataSet myds = new DataSet();
        GVNewsManager.DataSource = myds;
        myda.Fill(myds, "lis");
        //        GridView1.DataKeyNames = new string[] { "id" };
        GVNewsManager.DataBind();
        conn.Close();
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }
    protected void GVNewsManager_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        if (e.CommandName == "change")
        {
            NewsDAO newsDAO = new NewsDAO();
          //  Label1.Text = e.CommandArgument.ToString();
            newsDAO.changeState(e.CommandArgument.ToString());
            this.getDataSet();
        }
        if (e.CommandName == "deleteById") {
            NewsDAO newsDAO = new NewsDAO();
            newsDAO.deleteById(Convert.ToInt32(e.CommandArgument));
            this.getDataSet();
        }
        
    }

    protected void GVNewsManager_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
      
    }
    protected void GVNewsManager_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GVNewsManager.PageIndex = e.NewPageIndex;
        this.getDataSet();
    }
}
